from rest_framework.generics import ListAPIView
from . import models,serializers


# OrderingFilter排序， SearchFilter搜索
from rest_framework.filters import OrderingFilter,SearchFilter

# 分页
from .paginations import CarPageNumberPagination

# 分类筛选：django-fiter: filter_backends配置DjangoFilterBackend,再在filter_fields中配置分组筛选的字段
from django_filters.rest_framework import DjangoFilterBackend
from .filters import CarFilterSet



#搜车
class CarSelectListAPIView(ListAPIView):
    queryset = models.Car.objects.filter(
        is_delete=False, is_show=True
    ).order_by('price').all()
    serializer_class = serializers.CarListModelSerializer



    #配置过滤器
    filter_backends = [OrderingFilter, SearchFilter, DjangoFilterBackend]

    #参与排序的字段：ordering = -price, cc
    ordering_fields = ['price', 'id', 'cc']

    #参与搜索的字段：search=GMC（name字段中带GMC就ok）
    search_fields = ['model', 'brand', 'store']

    #参与分类筛选的字段：所有字段都可以，但是用于分组的字段更有意义
    filter_class = CarFilterSet

    #分页器
    pagination_class = CarPageNumberPagination



#按车牌查询
class CarBrandListAPIView(ListAPIView):
    queryset = models.CarBrand.objects.filter(
        is_delete=False, is_show=True
    ).all()
    serializer_class = serializers.CarBrandModelSerializer



#按车型查询
class CarTypeListAPIView(ListAPIView):
    queryset = models.CarModel.objects.filter(
        is_delete=False, is_show=True
    ).all()
    serializer_class = serializers.CarTypeModelSerializer
